package com.example.justbuy.action.buyer;


import com.example.justbuy.pojo.*;
import com.example.justbuy.service.HomeDaoService;
import com.example.justbuy.util.ProductUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;


@Controller
public class HomeAction {
    @Autowired
    private HomeDaoService homeDaoService;

    /**
     * 首页面跳转
     * @return
     * */
    @RequestMapping("/")
    public String HomePage(Model model,HttpServletRequest request){
        List<Image> listpl=homeDaoService.getAllIsPlayImage();
        List<Image> listrd=homeDaoService.getAllRecommendGoodsAndImages();
        List<Goods> listgd=homeDaoService.getAllTopGoods();
        List<Goods> listrdbear=homeDaoService.getAllRecommendAndGoodsTypeGoods();
        User user=(User)request.getSession().getAttribute("user");
        Integer num=null;
        if(user!=null){
            num=homeDaoService.getShoppingCartByUserid(user.getId());
        }
        model.addAttribute("listpl",listpl);
        model.addAttribute("listrd",listrd);
        model.addAttribute("listgd",listgd);
        model.addAttribute("listrdbear",listrdbear);
        model.addAttribute("num",num);
        return "index.html";
    }

    @RequestMapping("/searchlist.htm")
    public @ResponseBody List<Goods> searchlist(@RequestParam("content")String content,Model model){
        List<Goods> listgdl=homeDaoService.getAllGoodsNameByLike(content);
        return listgdl;
    }


    @RequestMapping("/pro_info.htm")
    public String product(@RequestParam("goodsId")int goodsId, Model model,HttpServletRequest request){
        Goods gd=homeDaoService.getGoodsBygoodsId(goodsId);
        List<ProductModel> listprd=null;
        if(gd.getModel()!=null){
            listprd= ProductUtil.getmodel(gd.getModel());
        }
        User user=(User)request.getSession().getAttribute("user");
        Integer num=null;
        if(user!=null){
            num=homeDaoService.getShoppingCartByUserid(user.getId());
        }
        List<Image> listim=homeDaoService.getImagesBygoodsId(goodsId);
        List<Evaluate> listel=homeDaoService.getAllEvaluateByGoodsId(goodsId);
        List<Image> listmd=homeDaoService.getAllImagesByGoodsIdAndDes(goodsId);
        Store st=homeDaoService.getStoreByGoodsId(goodsId);
        model.addAttribute("gd",gd);
        model.addAttribute("listprd",listprd);
        model.addAttribute("listim",listim);
        model.addAttribute("listel",listel);
        model.addAttribute("listmd",listmd);
        model.addAttribute("st",st);
        model.addAttribute("num",num);
        return "goods_info.html";
    }

    @RequestMapping("/goods_list.htm")
    public String goodslist(String searchname,Model model){
        searchname=searchname.trim();
        List<Goods> listge=homeDaoService.getAllGoodsAndEvaluateByGoodsName(searchname);
        model.addAttribute("listge",listge);
        model.addAttribute("searchname",searchname);
        return "goods_list.html";
    }

    @RequestMapping("/goods_list_zh.htm")
    public @ResponseBody List<Goods> goodslistzh(String searchname,Model model){
        List<Goods> listge=homeDaoService.getAllGoodsAndEvaluateByGoodsName(searchname);
        return listge;
    }

    @RequestMapping("/goods_list_xl.htm")
    public @ResponseBody List<Goods> goodslistxl(String searchname,Model model){
        List<Goods> listxl=homeDaoService.getAllGoodsAndEvaluateByGoodsNameAndXL(searchname);
        return listxl;
    }

    @RequestMapping("/goods_list_jg.htm")
    public @ResponseBody List<Goods> goodslistjg(String searchname,boolean flag,Model model){
        HashMap map=new HashMap();
        map.put("searchname",searchname);
        map.put("flag",flag);
        List<Goods> listjg=homeDaoService.getAllGoodsAndEvaluateByGoodsNameAndJG(map);
        return listjg;
    }

    @RequestMapping("/classify.htm")
    public String classify(Model model){
        return "classify.html";
    }

    @RequestMapping("/goods_list_js.htm")
    public String goodslistjs( Model model){
        List<Goods> listge=homeDaoService.getAllGoodsAndEvaluateJS();
        model.addAttribute("listge",listge);
        model.addAttribute("searchname","");
        model.addAttribute("jiushui",1);
        return "goods_list.html";
    }

    @RequestMapping("/goods_list_jszh.htm")
    public @ResponseBody List<Goods> goodslistjszh(String searchname,Model model){
        List<Goods> listge=homeDaoService.getAllGoodsAndEvaluateJS();
        return listge;
    }

    @RequestMapping("/goods_list_jsxl.htm")
    public @ResponseBody List<Goods> goodslistjsxl(Model model){
        List<Goods> listxl=homeDaoService.getAllGoodsAndEvaluateJSAndXL();
        return listxl;
    }

    @RequestMapping("/goods_list_jsjg.htm")
    public @ResponseBody List<Goods> goodslistjsjg(boolean flag,Model model){
        HashMap map=new HashMap();
        map.put("flag",flag);
        List<Goods> listjg=homeDaoService.getAllGoodsAndEvaluateJSAndJG(map);
        return listjg;
    }
}


